<template>
  <div id="salaryMain">
    <el-dialog
      :title="dialogtitle"
      :visible.sync="dialogVisible"
      width="98%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <personEdit @dialogHide="dialogHide($event)" :addType="addType" :personId="personId" :approvalId="approvalId" :readOnly="readonly" :timestamp="timestamp"></personEdit>
    </el-dialog>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item>工资管理</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs" style="height:60px;overflow:hidden">
    <!--工具条-->
      <el-form :inline="true" :model="filters">
        <div style="padding:10px">
          <el-radio-group v-model="showType" @change="changetype">
            <el-radio-button label="在任" border>
            </el-radio-button>
            <el-radio-button label="全部" border>
            </el-radio-button>
          </el-radio-group>
          <el-button-group v-if="state===1">
            <el-button type="primary" icon="el-icon-arrow-up" :loading="addLoading" @click="moveData(true)" :disabled="!moveUpFlag">上移</el-button>
            <el-button type="primary" icon="el-icon-arrow-down" :loading="addLoading" @click="moveData(false)" :disabled="!moveDownFlag">下移</el-button>
          </el-button-group>
          <!-- <el-radio-group v-model="showType" @change="changetype">
            <el-radio-button label="全部" border>
            </el-radio-button>
            <el-radio-button label="未提交" border>
            </el-radio-button>
            <el-radio-button label="已退回" border>
            </el-radio-button>
            <el-radio-button label="已提交" border>
            </el-radio-button>
            <el-radio-button label="已批准" border>
            </el-radio-button>
          </el-radio-group>
          <el-form-item label="姓名" label-width="50px">
            <el-input v-model="filters.xm" style="width:100px;" placeholder="关键字"></el-input>
          </el-form-item>
          <el-form-item label="性别" label-width="50px">
            <el-select v-model="filters.xb" placeholder="请选择" style="width:80px;">
              <el-option label="全部" value=""></el-option>
              <el-option
                v-for="item in xboptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" circle @click="search" icon="el-icon-search"></el-button>
          </el-form-item> -->
          <el-form-item style="float:right;">
            <el-button type="primary" @click="addnew('person')" icon="el-icon-circle-plus-outline">新增人员</el-button>
            <!-- <el-button type="success" @click="del()" icon="el-icon-upload2">批量提交</el-button> -->
          </el-form-item>
        </div>
      </el-form>
    </section>
    <section class="wboxd" style="margin-top:10px">
      <!--列表-->
      <el-table :data="list" highlight-current-row v-loading="listLoading" style="width: 100%;" :height="tableHeight" border
        @selection-change="handleSelectionChange"
        ref="multipleTable"
        >
        <el-table-column type="index" label="序号" width="50" align="center" class-name="tableth">
        </el-table-column>
        <el-table-column type="selection" width="45" v-if="state===1" key="selection">
        </el-table-column>
        <el-table-column prop="realname" label="姓名" width="80" align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="realname" key="state" label="任职状态" width="85" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-popconfirm title="确定设为在任吗？" v-if="scope.row.state===2" @confirm="setState(scope.row.id, 1)">
              <el-button slot="reference" type="text" style="color:red !important;font-weight:bolder" icon="el-icon-warning">不在任</el-button>
            </el-popconfirm>
            <el-popconfirm title="确定设为不在任吗？" v-else @confirm="setState(scope.row.id, 2)">
              <el-button slot="reference"  type="text" style="color:green !important;font-weight:bolder" icon="el-icon-success">在任</el-button>
            </el-popconfirm>
            <!-- <span style="color:green;font-weight:bolder">在任</span> -->
          </template>
        </el-table-column>
        <el-table-column align="center" v-for="item in yearList" :key="'year'+item" :prop="'year'+item" :label="''+item">
          <template slot-scope="scope" v-if="scope.row.approvalList">
            <el-tooltip
              v-for="it in scope.row.approvalList.filter(approval=>approval.theYear===item)"
              :key="'approval'+it.id"
              :open-delay="600"
              class="item"
              effect="dark"
              :content="(it.theType===1?'审批':'备案')+' '+stateList.find(state=>state.value===it.state).label">
              <el-button :type="stateList.find(state=>state.value===it.state).type" size="mini" round :icon="it.theType===1?'el-icon-s-claim':'el-icon-s-cooperation'" style="margin:5px;color:#fff !important" @click="showApproval(it.id)" ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200px">
          <template slot-scope="scope">
            <el-dropdown split-button type="primary" @click="addnew('approval',scope.row.id)" size="small" v-if="scope.row.state===1" :loading="btnLoading">
              新增工资变动
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="detail('person', scope.row.id)" >修改人员信息</el-dropdown-item>
                <el-dropdown-item @click.native="remove(scope.row.id)" >删除人员信息</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <el-dropdown split-button type="primary" @click="detail('person', scope.row.id)" size="small" v-else :loading="btnLoading">
              修改人员信息
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="remove(scope.row.id)">删除人员信息</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </section>
  </div>
</template>

<script>
import personEdit from './edit'
export default {
  components: {
    personEdit
  },
  data () {
    return {
      btnLoading: false,
      yearList: [],
      stateList: [
        {label: '未提交', value: 0, type: 'info'},
        {label: '已提交 ', value: 1, type: 'primary'},
        {label: '审批通过', value: 2, type: 'success'},
        {label: '退回', value: 3, type: 'danger'}
      ],
      approvalId: null,
      multipleSelection: [],
      addLoading: false,
      moveUpFlag: false,
      moveDownFlag: false,
      state: 1,
      addType: 'person',
      webDataDialogVisible: false,
      dataUrl: this.$http.defaults.youngAPI + `api/busFormalPerson/queryAll`,
      dataPara: {},
      xboptions: [],
      shfloptions: [],
      lyfloptions: [],
      pysyjylxoptions: [],
      showCksj: false,
      canedit: false,
      showType: '在任',
      moreShow: false,
      exportLoading1: false,
      cllxDic: [],
      wtlyDic: [],
      cljdjgDic: [],
      yxqDic: [],
      roleAdmin: true,
      roleEdit: true,
      roleChart: true,
      settype: '',
      setAuthText: '',
      roleSubmitLoading2: false,
      isIndeterminate2: false,
      roleSubmitLoading: false,
      role: [],
      checkAll2: false,
      checkAll: false,
      isIndeterminate: false,
      setRole: false,
      setrole0: false,
      // roles: [{label: '查看', value: 1}, {label: '编辑', value: 2}, {label: '分析', value: 3}],
      roles: ['查看', '编辑', '分析'],
      roles2: [],
      roleuser: [],
      userlist: [],
      processId: '',
      drawer: false,
      authUser: '',
      timestamp: '',
      noRoleCode: '***',
      noRoleText: '***',
      filters: {
        sscs: null,
        dwmc: '',
        sfzh: '',
        xb: '',
        xm: '',
        zt: 0
      },
      listLoading: false,
      ids: [],
      tableHeight: 100,
      list: [
      ],
      pageNum: 1,
      pageSize: 20,
      totalnum: 10,
      personId: 0,
      dialogtitle: '新增',
      dialogVisible: false,
      readonly: false,
      total: 0,
      Isace: 'asc',
      Sortkey: '',
      changeSizeFlag: false,
      myloading: false,
      sscsArr: [],
      totalPage: 0,
      orderloading: false
    }
  },
  methods: {
    getAllStandard () {
      let para = {
        pageNum: 1,
        pageSize: 10000
      }
      this.$http
        .post(this.$http.defaults.salaryAPI + `api/salaryBusStandardDetail/queryByPage`, para)
        .then(response => {
          if (response.data.success) {
            localStorage.setItem('standard', JSON.stringify(response.data.result.records))
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    handleSelectionChange (val) {
      this.moveUpFlag = false
      this.moveDownFlag = false
      this.multipleSelection = val
      if (this.multipleSelection.length === 1) {
        this.moveUpFlag = true
        this.moveDownFlag = true
        if (this.list.findIndex(item => item.id === this.multipleSelection[0].id) === 0) {
          this.moveUpFlag = false
        }
        if (this.list.findIndex(item => item.id === this.multipleSelection[0].id) === this.list.length - 1) {
          this.moveDownFlag = false
        }
      }
    },
    setState (id, state) {
      this.$http.post(this.$http.defaults.salaryAPI + `api/salaryBusPerson/updateState`, {id: id, state: state}).then(response => {
        if (response.data.success === true) {
          this.$message({
            message: '设置成功',
            type: 'success'
          })
        } else {
          console.log('设置失败:' + response.data.message)
          this.$message({
            message: '设置失败:' + response.data.message,
            type: 'error'
          })
        }
        this.getList()
      })
    },
    remove (id) {
      this.$confirm('确认删除该信息吗?', '提示', {
        type: 'error'
      }).then(() => {
        this.btnLoading = true
        this.$http.post(this.$http.defaults.salaryAPI + `api/salaryBusPerson/remove`, {ids: [id]}).then(response => {
          this.btnLoading = false
          if (response.data.success === true) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            console.log('删除失败:' + response.data.message)
            this.$message({
              message: '删除失败:' + response.data.message,
              type: 'error'
            })
          }
          this.getList()
        })
      }).catch(() => {
      })
    },
    getList (id) {
      let para = {
        state: this.state
      }
      this.listLoading = true
      this.$http
        .post(this.$http.defaults.salaryAPI + `api/salaryBusPerson/listPersons`, para)
        .then(response => {
          if (response.data.success) {
            this.listLoading = false
            this.list = response.data.result
            this.yearList = []
            response.data.result.forEach(element => {
              if (element.approvalList && element.approvalList.length > 0) {
                element.approvalList.forEach(ele => {
                  if (!this.yearList.includes(ele.theYear)) {
                    this.yearList.push(ele.theYear)
                  }
                })
              }
            })
            this.yearList.sort((a, b) => b - a)
            // this.list = [{
            //   id: 8,
            //   state: '未提交',
            //   realname: '李丽',
            //   idCardNo: '340401197906100971',
            //   jobDate: '2005-11-01',
            //   salaryDate: '2018-10-10',
            //   jobClass: '管理岗位',
            //   jobLevel: '管理四级',
            //   salaryLevel: '30级',
            //   reason: '审批：新提任副局级干部'
            // }, {
            //   id: 8,
            //   state: '已退回',
            //   realname: '赵宝刚',
            //   idCardNo: '340401197906100971',
            //   jobDate: '2011-08-11',
            //   salaryDate: '2016-09-10',
            //   jobClass: '专业技术岗位',
            //   jobLevel: '专业技术四级',
            //   salaryLevel: '25级',
            //   reason: '备案：因享受劳模待遇高定工资薪级',
            //   remarks: '缺少考核证明文件附件'
            // }, {
            //   id: 8,
            //   state: '已提交',
            //   realname: '张三',
            //   idCardNo: '340401197906100971',
            //   jobDate: '2001-01-01',
            //   salaryDate: '2021-11-11',
            //   jobClass: '专业技术岗位',
            //   jobLevel: '专业技术三级',
            //   salaryLevel: '31级',
            //   reason: '备案：因岗位等级晋升需要晋升基本工资'
            // }]
            if (id) {
              this.toggleSelection(id)
            }
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    addnew (type, personId) {
      this.readonly = false
      this.dialogtitle = '新增'
      this.personId = personId
      this.approvalId = null
      this.dialogVisible = true
      this.addType = type
      this.timestamp = new Date().getTime()
    },
    detail (type, personId, approvalId) {
      this.readonly = true
      this.dialogtitle = '查看'
      this.personId = personId
      this.approvalId = approvalId
      this.dialogVisible = true
      this.addType = type
      this.timestamp = new Date().getTime()
    },
    showApproval (approvalId) {
      this.readonly = true
      this.dialogtitle = '查看'
      this.approvalId = approvalId
      this.dialogVisible = true
      this.addType = 'approval'
      this.timestamp = new Date().getTime()
    },
    edit (id) {
      this.readonly = false
      this.dialogtitle = '修改'
      this.personId = id
      this.dialogVisible = true
      this.timestamp = new Date().getTime()
    },
    moveData (flag) {
      let para = {
        id: this.multipleSelection[0].id,
        up: flag
      }
      this.addLoading = true
      this.$http.post(this.$http.defaults.salaryAPI + `api/salaryBusPerson/updateOrder`, para).then(response => {
        console.log(response.data.success)
        if (response.data.success === true) {
          this.addLoading = false
          this.getList(this.multipleSelection[0].id)
        } else {
          console.log('调整顺序失败:' + response.data.message)
          this.$message({
            message: '调整顺序失败:' + response.data.message,
            type: 'error'
          })
          this.addLoading = false
        }
      }).catch(error => {
        console.log(error)
        this.$message({
          message: '调整顺序失败:' + error,
          type: 'error'
        })
        this.addLoading = false
      })
    },
    changetype () {
      this.state = this.showType === '在任' ? 1 : null
      this.getList()
    },
    toggleSelection (id) {
      this.$nextTick(() => {
        if (id) {
          let newrow = this.list.find(item => item.id === id)
          console.log(newrow)
          this.$refs.multipleTable.toggleRowSelection(newrow)
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      })
    },
    dialogHide (flag) {
      this.getList()
      if (flag) {
        this.dialogVisible = false
      }
    },
    // 查询
    search () {
      if (this.filters.zt === 0) {
        this.showCksj = false
      } else {
        this.showCksj = true
      }
      this.pageNum = 1
      this.getList()
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      let myheight = 220
      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - myheight
      })
    }
  },
  mounted () {
    this.getList()
    this.tableresize(this)
    window.onresize = () => {
      this.tableresize(this)
    }
    this.getAllStandard()
  }
}
</script>
<style>
#salaryMain .el-drawer__wrapper{
  width: 100%;
  height: 100%;
  position: fixed !important;
  overflow: hidden;
}
  .wboxd .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  .wboxd .el-table td, .el-table th{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体'
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #testlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  #cluereport .el-table td{
    border-right: 1px solid #95B8E7;
    border-bottom: 1px solid #95B8E7;
  }
</style>
<style scoped>
  #salaryMain h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  #salaryMain .wboxs{
    width: 100%;
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
    padding:0;
  }
  .wboxd{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
